using System;
using System.Collections.Generic;
using UnityEngine;

public class ActivityManager
{
	public const string START_TIME_KEY = "StartActivity";

	private static ActivityManager _instance;

	public const int DURATION_TIME = 172800;

	private List<ActivityPrize> _prizeList;

	private DateTime _startTime;

	public List<ActivityPrize> prizeList
	{
		get
		{
			return _prizeList;
		}
		set
		{
			_prizeList = value;
		}
	}

	public static ActivityManager instance
	{
		get
		{
			if (_instance == null)
			{
				_instance = new ActivityManager();
			}
			return _instance;
		}
	}

	private ActivityManager()
	{
		_prizeList = Utils.LoadXML<ActivityPrize>("Cfg/ActivityPrize");
	}

	public void Start()
	{
		_startTime = DateTime.Now;
		PlayerPrefs.SetString("StartActivity", DateTime.Now.ToString());
	}

	public int GetLeftTime()
	{
		if (PlayerPrefs.HasKey("StartActivity"))
		{
			return (int)(172800.0 - (DateTime.Now - DateTime.Parse(PlayerPrefs.GetString("StartActivity"))).TotalSeconds);
		}
		return 172800;
	}

	public string GetLeftTimeDesc()
	{
		int leftTime = GetLeftTime();
		if (leftTime > 0)
		{
			int num = leftTime / 3600;
			int num2 = leftTime % 3600 / 60;
			int num3 = leftTime % 3600 % 60;
			return $"{num}h {num2}m {num3}s";
		}
		return "0s";
	}

	public void ReOpen()
	{
		if (GetLeftTime() < 0 && !IsTakenAllPrize())
		{
			Start();
		}
	}

	public ActivityPrize GetPrize(int id)
	{
		if (_prizeList == null)
		{
			return null;
		}
		for (int i = 0; i < _prizeList.Count; i++)
		{
			if (_prizeList[i].id == id)
			{
				return _prizeList[i];
			}
		}
		return null;
	}

	public bool HavePrize()
	{
		for (int i = 0; i < _prizeList.Count; i++)
		{
			ActivityPrize activityPrize = _prizeList[i];
			if (CanTakePrize(activityPrize.id))
			{
				return true;
			}
		}
		return false;
	}

	public bool IsTakenAllPrize()
	{
		for (int i = 0; i < _prizeList.Count; i++)
		{
			ActivityPrize activityPrize = _prizeList[i];
			if (!IsTakenPrize(activityPrize.id))
			{
				return false;
			}
		}
		return true;
	}

	public bool CanTakePrize(int id)
	{
		if (_prizeList == null)
		{
			return false;
		}
		ActivityPrize prize = GetPrize(id);
		if (prize == null)
		{
			return false;
		}
		if (ConstantData.missionRecords.GetTotalStar() < prize.star)
		{
			return false;
		}
		if (PlayerPrefs.HasKey("ActivityPrize" + id))
		{
			return false;
		}
		return true;
	}

	public void TakePrize(int id)
	{
		if (!CanTakePrize(id))
		{
			return;
		}
		ActivityPrize prize = GetPrize(id);
		if (string.IsNullOrEmpty(prize.items))
		{
			return;
		}
		string[] array = prize.items.Split(',');
		for (int i = 0; i < array.Length; i++)
		{
			string[] array2 = array[i].Split('_');
			int infoId = 0;
			int count = 0;
			if (array2.Length >= 1)
			{
				infoId = Convert.ToInt32(array2[0]);
			}
			if (array2.Length >= 2)
			{
				count = Convert.ToInt32(array2[1]);
			}
			ItemMgr.instance.AddItem(infoId, count);
			PlayerPrefs.SetInt("ActivityPrize" + id, 1);
		}
		ItemMgr.instance.AddItem(8);
	}

	public bool IsTakenPrize(int id)
	{
		ActivityPrize prize = GetPrize(id);
		if (prize == null)
		{
			return true;
		}
		return PlayerPrefs.HasKey("ActivityPrize" + id);
	}

	public float GetStarPercent()
	{
		int totalStar = ConstantData.missionRecords.GetTotalStar();
		int num = -1;
		for (int i = 0; i < _prizeList.Count; i++)
		{
			if (totalStar >= _prizeList[i].star)
			{
				num = i;
			}
		}
		if (num < _prizeList.Count - 1)
		{
			float a = (num >= 0) ? _prizeList[num].percent : 0f;
			int num2 = (num >= 0) ? _prizeList[num].star : 0;
			float percent = _prizeList[num + 1].percent;
			int star = _prizeList[num + 1].star;
			return Mathf.Lerp(a, percent, (float)(totalStar - num2) * 1f / (float)(star - num2));
		}
		return _prizeList[_prizeList.Count - 1].percent;
	}
}
